/*
 * Tencent is pleased to support the open source community by making
 * MMKV available.
 *
 * Copyright (C) 2018 THL A29 Limited, a Tencent company.
 * All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *       https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tencent.mmkv;


import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.bundle.ElementName;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;

public class MyService_1 extends BenchMarkBaseService {
    public static final String CMD_PREPARE_ASHMEM = "cmd_prepare_ashmem";
    private static final String CALLER = "MyService_1";
    public HiLogLabel service_1_label=new HiLogLabel(HiLog.LOG_APP,0x568797,"MyService_1");
    @Override
    protected void onCommand(Intent intent, boolean restart, int startId) {
        HiLog.info(service_1_label,"MyService_1 onStartCommand:");
        if (intent != null) {
            String cmd = intent.getStringParam(CMD_ID);
            HiLog.info(service_1_label,"----MyService_1 onStartCommand:" + cmd);
            if (cmd != null) {
                if (cmd.equals(CMD_READ_INT)) {
                    super.batchReadInt(CALLER);
                } else if (cmd.equals(CMD_READ_STRING)) {
                    super.batchReadString(CALLER);
                } else if (cmd.equals(CMD_WRITE_INT)) {
                    super.batchWriteInt(CALLER);
                } else if (cmd.equals(CMD_WRITE_STRING)) {
                    try {
                        super.batchWriteString(CALLER);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                } else if (cmd.equals(CMD_PREPARE_ASHMEM)) {
                    Intent i = new Intent();
                    Operation operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName("com.tencent.mmkv")
                            .withAbilityName("com.tencent.mmkv.MyService_1")
                            .build();
                    i.setOperation(operation);
                    // 连接Service
                    connectAbility(i, connection);

                } else if (cmd.equals(CMD_PREPARE_ASHMEM_BY_CP)) {
                    super.prepareAshmemMMKVByCP();
                }
            }
        }

        super.onCommand(intent, restart, startId);
    }
    // 创建连接回调实例
    private IAbilityConnection connection = new IAbilityConnection() {
        // 连接到Service的回调
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
            // 在这里开发者可以拿到服务端传过来IRemoteObject对象，从中解析出服务端传过来的信息
//            IMyIdlInterface ashmemMMKV=MyIdlInterfaceStub.asInterface(iRemoteObject);
//            try {
//                ParcelableMMKV parcelableMMKV = ashmemMMKV.getAshmemMMKV();
//                if (parcelableMMKV != null) {
//                    m_ashmemMMKV = parcelableMMKV.toMMKV();
//                    if (m_ashmemMMKV != null) {
////                        HiLog.info(label,"MMKV ashmem bool: " + m_ashmemMMKV.decodeBool("bool"));
//                    }
//                }
//            } catch (RemoteException e) {
//                e.printStackTrace();
//            }

        }

        // 断开与连接的回调
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
        }
    };


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
//        HiLog.info(label,"onCreate MyService_1");
    }


    @Override
    protected void onStop() {
        super.onStop();
//        HiLog.info(label, "onDestroy MyService_1");
    }





}
